package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.dto.system.SysUserDto;
import com.atguigu.spzx.model.entity.system.SysUser;
import com.atguigu.spzx.model.vo.common.Result;
import com.github.pagehelper.PageInfo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@Tag(name = "用户管理模块")
@RestController
@RequestMapping("/admin/system/user")
public class SysUserController {
    @Resource
    SysUserService sysUserService;
    @Operation(summary = "删除")
    @DeleteMapping("{id}")
    public Result deleteById(@PathVariable("id")Long id){
        //删除之前查询用户头像
        sysUserService.deleteById(id);
        //删除成功：删除用户头像
        return Result.ok(null);
    }
    @Operation(summary = "新增")
    @PostMapping
    public Result save(@RequestBody SysUser sysUser){
        sysUserService.save(sysUser);
        return Result.ok(null);
    }

    @Operation(summary = "根据id查询")
    @GetMapping("{id}")
    public Result<SysUser> getById(@PathVariable("id")Long id){

        return Result.ok(sysUserService.getById(id));
    }

    @Operation(summary = "更新")
    @PutMapping
    public Result updateById(@RequestBody SysUser sysUser){
        sysUserService.updateById(sysUser);
        return Result.ok(null);
    }

    /*
        前端提交参数的方式：
            1、请求头：  后端 @RequestHeader()
            2、请求参数：
                url?k=v&k2=v2&k2=v3
                后端接收：
                    >直接获取参数： @RequestParam("k") String k
                    >pojo入参：  创建javabean 提供gettersetter方法，属性名+类型和请求参数一致 可以自动接收
             3、请求体： 不能使用get方式
                {k:v,k2:v2}  ==> @RequestBody javabean接收     原理 是使用jackson转换器将json转为对象

     */
    @Operation(summary = "条件查询用户分页数据")
    @GetMapping("page/{pageNum}/{pageSize}")
    //返回值泛型 对应的是前端需要展示的数据
    public Result<PageInfo<SysUser>> page(@PathVariable("pageNum")Integer pageNum,
                                          @PathVariable("pageSize")Integer pageSize,
                                          SysUserDto sysUserDto){//pojo入参
        PageInfo<SysUser> pageInfo = sysUserService.page(pageNum,pageSize,sysUserDto);
        return Result.ok(pageInfo);
    }
}
